import axios from "axios";
import loading from "../plugins/loading";
const config = {
  // baseURL: process.env.baseURL,
  baseURL: "http://127.0.0.1:9999/",
  // baseURL: "http://180.76.244.163:3003/",
  // timeout: 1000,
  headers: {
    "Content-Type": "application/x-www-form-urlencoded",
  },
};

const api = axios.create(config);
// 默认 post 请求，使用 application/json 形式
api.defaults.headers.post["Content-Type"] =
  "application/x-www-form-urlencoded;charset=UTF-8";
// 请求前
api.interceptors.request.use(
  (config) => {
    // 打开loading
    loading.openLoading();
    // 默认请求头
    let useInfo = JSON.parse(sessionStorage.getItem("useInfo") || "{}");
    const headers = {
      authorization: useInfo.token,
    };
    config.headers = Object.assign(headers, config.headers || {});
    return config;
  },
  (error) => {
    loading.closeLoading({ default: true });
    return Promise.reject(error);
  }
);
// http response 拦截器
api.interceptors.response.use(
  (response) => {
    loading.closeLoading({ default: true });
    const res = response.data;
    return res;
  },
  (error) => {
    loading.closeLoading({ default: true });
    return Promise.reject(error);
  }
);

export default api;
